<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <meta name="format-detection" content="address=no" />
    <meta name="format-detection" content="date=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>扫码验票</title>
    <link href="../css/aui.css" rel="stylesheet" type="text/css">
</head>

<body style="background:#000;">

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/service/service-app.js"></script>
<script type="text/javascript" src="../script/service/service-logic.js"></script>
<script type="text/javascript" src="../script/service/service-im.js"></script>
<script type="text/javascript">
    var FNScanner;
    var _type
    var $S;
    var USER;

    var isKeyUp = true;
    apiready = function() {
        USER = USER();
        $S = SERVICE();
        FNScanner = api.require('FNScanner');
        //_type  1 首页扫码解锁课程   2  我的，教师扫码登录pc端
        _type = api.pageParam.type; 
        openScanner();
        // 摄像头权限
        fnHasPermission(openScanner, '1');
    };


    function openScanner() {
        // APP回到前台
        api.addEventListener({
            name: 'resume'
        }, function(ret, err) {
            FNScanner.onResume();
        });
        // APP到后台
        api.addEventListener({
            name: 'pause'
        }, function(ret, err) {
            FNScanner.onPause();
        });
        // 打开扫码
        FNScanner.openView({
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth - 0,
                h: api.winHeight - 0
            },
            autorotation: false,
        }, function(ret, err) {
            if (ret) {
                if (ret.eventType == "show") {
                    setTimeout(function() {
                        var tParam = {
                            rect: {
                                x: 0,
                                y: 0,
                                w: api.winWidth,
                                h: api.winHeight
                            },
                            reload: true,
                            name: 'scanner_frm',
                            url: './scanner_frm.html',
                            bgColor: 'rgba(0,0,0,0)',
                            pageParam: {
                                _type: _type
                            }
                        };
                        fnOpenFrame(tParam);
                    }, 300)
                }

                if (ret.eventType == "success") {
                    // 扫码成功
                    if (isKeyUp) {
                        // FNScanner.closeView();
                        FNScanner.onPause();
                        isKeyUp = false;
                        api.bringFrameToFront({
                            from: 'scanner_frm',
                        });
                        var content = ret.content;
                        console.log(content+'======qrcode')
                        if (_type == "1") {
                            // 解锁课程  
                            api.sendEvent({
                                name: 'scanCourseCode',
                                extra: {
                                    code: content
                                }
                            });
                            setTimeout(function() {
                                api.closeWin();
                            }, 100);
                        } else if (_type = "2") { //验票端
                            // 登录pc端
                            fnToast('扫码成功');
                            setTimeout(function() {
                                api.closeWin();
                            }, 1000);
                        }
                    }

                } else if (ret.eventType == "cameraError") {
                    fnToast('相机打开错误，请重试');
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                }
            }
        });
    }

    //打开手电筒
    var lightStatus = "on";

    function fnOpenLight() {
        FNScanner.switchLight({
            status: lightStatus
        });
        if (lightStatus == "on") {
            lightStatus = "off"
        } else {
            lightStatus = "on"
        }
    }
</script>

</html>
